# -*- coding: utf-8 -*-
import time
import win32gui, win32ui, win32con,win32api
import pyautogui as pag

def get_point():
    print('正在采集坐标1，请将鼠标移动到该点')
    print('延时5秒钟')
    time.sleep(5)
    x1,y1 = pag.position() #返回鼠标的坐标
    print('坐标1采集结束...')
    print('采集成功，坐标为：',(x1,y1))
    
    print('正在采集坐标2，请将鼠标移动到该点')
    print('延时5秒钟')
    time.sleep(5)
    x2, y2 = pag.position()  # 返回鼠标的坐标
    print('采集成功，坐标为：',(x2,y2))
    print('坐标2采集结束...')
    
    #os.system('cls')#清除屏幕
    w = abs(x1 - x2)
    h = abs(y1 - y2)
    x = min(x1, x2)
    y = min(y1, y2)
    print('w:' + str(w) + 'h:' + str(h) + 'x:' + str(x) + 'y:' + str(y))
    return w,h,x,y


def window_capture(result,filename):
    w,h,x,y = result
    hwnd = 0
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    #获取监视器信息
    MoniterDev = win32api.EnumDisplayMonitors(None, None)
    # w = MoniterDev[0][2][2]
    # h = MoniterDev[0][2][3]
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0, 0), (w, h), mfcDC, (x, y), win32con.SRCCOPY)
    saveBitMap.SaveBitmapFile(saveDC, filename)

if __name__ == '__main__':
    result =  get_point()
    window_capture(result,'jietu.jpg')